<?php
function format_number( $val )
{
 $val=trim($val);
 $svals = explode( "." , $val ); //????????????
 
 $sval = $svals[0]; //?????????
 $st=$svals[1];
 $n = 0;
 $result = "";
 for( $i = strlen( $sval ) - 1 ; $i >= 0 ; $i-- )
 {  if ( $sval[$i] == "." )
  {   $n = -1;  }
  else if ( $n == 3 )
  {
   $result = ",$result"; //??? comma
   $n = 0;
  }
  $n++;
  $result = $sval[$i].$result;
 }
if(strlen($svals[1])==0 )$svals[1]="00";
else  if(strlen($svals[1])==1 )$svals[1]=$svals[1]."0";
else  if(strlen($svals[1])==2 )$svals[1]=$svals[1];

 return ( count( $svals ) > 1 ) ? $result.'.'.$svals[1] : $result;
}
function changeshow($id)
{
 	$ids=proDC("show",$id);
    $qr=mysql_query("SELECT proShow FROM  sme_product WHERE sme_product.productId ='$ids'") ;
	if(mysql_num_rows($qr)==1)
	  {
	  	  $rs=mysql_fetch_array($qr);
		  if($rs[0])$stt=0;else $stt=1;
		  if($ids) { 
		  mysql_query("UPDATE sme_product SET proShow = '$stt' WHERE sme_product.productId =$ids LIMIT 1 ;");
		   redirect("product.php#a$ids");	  }
	  }
	  else redirect("product.php");	
}
function del($id)
{
 	$ids=proDC("del",$id);
    $qr=mysql_query("SELECT productId FROM  sme_product WHERE sme_product.productId ='$ids'") ;
	if(mysql_num_rows($qr)==1)
	  {
		  if($ids) { 
		  mysql_query("UPDATE sme_product  SET sme_product.delete = '1' WHERE sme_product.productId =$ids LIMIT 1 ;");
		   redirect("product.php");	  }
	  }
	  else redirect("product.php");	
}
function sqlCmdNumRow($whcondit="")//--
{
return mysql_num_rows(mysql_query("SELECT productId FROM sme_product  LEFT JOIN sme_brand  	ON brandId = proBrandId  
 LEFT JOIN  sme_category 	ON categoryId = proCategoryId LEFT JOIN sme_tax 	ON taxid = proTax   WHERE $whcondit sme_product.delete=0"));
}
function sqlCmdQr($start,$limit=100,$whcondit="")//--
{
return "SELECT sme_product . * , category,brandName,taxrate
 FROM sme_product
  LEFT JOIN sme_brand 
  	ON brandId = proBrandId  
 LEFT JOIN  sme_category 
	ON categoryId = proCategoryId
 LEFT JOIN sme_tax
 	ON taxid = proTax  
 WHERE $whcondit sme_product.delete=0
 ORDER BY  productId ASC
	 LIMIT $start,$limit";
}
function getCate()
{
	$sql= 'SELECT categoryId,category,parent_cate,offset FROM sme_category ORDER BY offset ASC ,categoryId ASC '; 
		return  mysql_query($sql);
}
function getBrand()
{
	$sql="SELECT brandId,brandName FROM sme_brand  ORDER BY brandId ASC";
	return  mysql_query($sql);
}
function proEC($action,$txt)
{	return base64_encode("$action=".base64_encode($txt." PDCT") ) ;}
function proDC($action,$txt)
{
 $decode =explode("=",base64_decode($txt));
 if($decode[0]==$action)
 {   $DC=explode(" ",base64_decode($decode[1]));
 		if($DC[1]=="PDCT")return addslashes(strip_tags($DC[0]));
		else return FALSE;
 }
 else return FALSE;
}
function dateTotxt($txt)
{
	$date=explode(" ",$txt);
	$order=explode("-",$date[0]);
	return $order[2]."/". $order[1]."/". $order[0]." ".$date[1];
}
function YmdHisTostmp($time){ // เวลาในรูปแบบ มาตรฐาน 2006-03-27 21:39:12
 $timeArr["Y"]= substr($time,2,2);
 $timeArr["M"]= substr($time,5,2);
 $timeArr["D"]= substr($time,8,2);
 $timeArr["h"]= substr($time,11,2);
 $timeArr["m"]= substr($time,14,2);
 $timeArr["s"]= substr($time,17,2);
 return  mktime($timeArr["h"], $timeArr["m"], $t1Arr["s"], $timeArr["M"], $timeArr["D"], $timeArr["Y"]);
}
function dateNotZero($txt)
{
	$date=explode(" ",$txt);
	$order=explode("-",$date[0]);
	if( $order[2]=="00" || $order[1]=="00" || $order[0]=="0000")
	return FALSE;
	else return TRUE;
}
function redirect($path)
{
echo "<meta http-equiv='refresh' content='0;URL=$path'>";	 exit();
}


?>